package com.link.base.tencent.corpwx.addressbook.service;

import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.addressbook.model.Department;
import com.link.base.tencent.corpwx.addressbook.util.DeptUtil;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 企业微信 - 部门服务 - API实现
 *
 * @author 欧阳振强
 * @date 2019/1/3
 */
@Service
public class AddressBookDeptServiceImpl extends BasicServiceImpl<Department> implements AddressBookDeptService {
    private static final String Y_FLAG = "Y";
    @Resource
    private AgentService agentService;

    @Override
    public String newWxDept(String agentId, Department dept) throws Exception {
        String accessToken = agentService.getExistAccessToken(agentId);
        JSONObject obj = DeptUtil.newWxDept(accessToken, dept);
        return obj.getString("id");
    }

    @Override
    public void updateWxDept(String agentId, Department dept) throws Exception {
        String accessToken = agentService.getExistAccessToken(agentId);
        DeptUtil.updateWxDept(accessToken, dept);
    }

    @Override
    public void deleteWxDept(String agentId, String deptId, String isDelChild) throws Exception {
        String accessToken = agentService.getExistAccessToken(agentId);
        if (StringUtils.equals(Y_FLAG, isDelChild)) {
            DeptUtil.deleteChildWxDept(accessToken, deptId);
        }
        DeptUtil.deleteWxDept(accessToken, deptId);
    }
}
